package model;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;

import model.command.Brz;
import model.command.Clr;
import model.command.Inc;
import model.command.Load;
import model.command.Store;

public class MinCommandSet implements Iterable<Command> {

	private MinCommandSet(String name) {
		// no one else should define MinCommandSets
		this.name = name;
	}

	private static final ArrayList<MinCommandSet> MIN_COMMAND_SETS = new ArrayList<MinCommandSet>();

	static {
		// Define Minimum Command sets
		MinCommandSet minset1 = new MinCommandSet("Default Set");
		minset1.addCommand(1, new Brz());
		minset1.addCommand(2, new Clr());
		minset1.addCommand(3, new Inc());
		minset1.addCommand(4, new Load());
		minset1.addCommand(5, new Store());

		MIN_COMMAND_SETS.add(minset1);
	}

	public static String[] getMinCommandSets() {
		String[] sets = new String[MIN_COMMAND_SETS.size()];
		for (int i = 0; i < MIN_COMMAND_SETS.size(); i++) {
			sets[i] = MIN_COMMAND_SETS.get(i).getName();
		}
		return sets;
	}

	public static MinCommandSet getMinCommandSet(String name) {
		for (MinCommandSet set : MIN_COMMAND_SETS) {
			if (set.getName().equals(name)) {
				return set;
			}
		}
		return null;
	}

	private String name;

	public HashMap<Integer, Command> command = new HashMap<Integer, Command>();

	public String getName() {
		return name;
	}

	public HashMap<Integer, Command> getCommands() {
		return command;
	}

	public void addCommand(int opcode, Command command) {
		this.command.put(opcode, command);
	}

	public Command getCommandForOpcode(int opcode) {
		return command.get(opcode);
	}

	@Override
	public Iterator<Command> iterator() {
		return command.values().iterator();
	}
}
